import { CommandHandler, ICommandHandler, EventBus } from '@nestjs/cqrs'
import { CreateAvatarCommand } from './create-avatar.command'
import { Repository } from 'typeorm'
import { AvatarEntity } from '../entities/avatar.mysql.entity'
import { Inject } from '@nestjs/common'
import { AvatarCreateEvent } from '../events/create-avatar.event'
@CommandHandler(CreateAvatarCommand)
export class CreateAvatarCommandHandler implements ICommandHandler<CreateAvatarCommand>{
  constructor(
    @Inject('AVATAR_REPOSITORY')
    private readonly avatarRepository: Repository<AvatarEntity>,
    private readonly eventBus: EventBus
  ) { }
  async execute(command: CreateAvatarCommand) {
    const avatar = await this.avatarRepository.save(command.params)
    /**
     * 发布创建多尺寸头像事件
     */
    this.eventBus.publish(new AvatarCreateEvent(avatar))
    return avatar
  }
}